package com.ruoyi.system.service.impl;

import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.AirPowerInfo;
import com.ruoyi.system.mapper.AirPowerInfoMapper;
import com.ruoyi.system.service.IAirPowerInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;

/**
 * 参数管理模块
 * @author ytl
 */
@Service
public class AirPowerInfoServiceImpl implements IAirPowerInfoService {

    @Autowired
    public AirPowerInfoMapper airPowerInfoMapper;

    private static final Logger log = LoggerFactory.getLogger(AirPowerInfoServiceImpl.class);

    @Override
    @Transactional
    public int insertAirPowerInfo(AirPowerInfo airPowerInfo) {
        int rows = airPowerInfoMapper.insertAirPowerInfo(airPowerInfo);
        return rows;
    }

    @Override
    public List<AirPowerInfo> listAirPowerInfo(AirPowerInfo airPowerInfo) {
        return airPowerInfoMapper.listAirPowerInfo(airPowerInfo);
    }

    @Override
    public List<AirPowerInfo> listAirPowerInfoAll() {
        return airPowerInfoMapper.listAirPowerInfoAll();
    }

    @Override
    @Transactional
    public int deleteAirPowerInfo(AirPowerInfo airPowerInfo) {
        return airPowerInfoMapper.deleteAirPowerInfo(airPowerInfo);
    }

    @Override
    @Transactional
    public int updeteAirPowerInfo(AirPowerInfo airPowerInfo) {
        return airPowerInfoMapper.updeteAirPowerInfo(airPowerInfo);
    }

    @Override
    public List<AirPowerInfo> listFuelTenantInfo() {
        return airPowerInfoMapper.listFuelTenantInfo();
    }

    @Override
    public String importAirPowerInfo(List<AirPowerInfo> AirPowerInfoList, Boolean isUpdateSupport, String operName) {
        if (StringUtils.isNull(AirPowerInfoList) || AirPowerInfoList.size() == 0)
        {
            throw new BusinessException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (AirPowerInfo airPowerInfo:AirPowerInfoList){
            try {
                    List<AirPowerInfo> list = airPowerInfoMapper.listAirPowerInfo(airPowerInfo);
                    if(list.size()==0){
                        airPowerInfo.setPowerUUID(UUID.randomUUID().toString());
                        airPowerInfo.setCreateBy(operName);
                        this.insertAirPowerInfo(airPowerInfo);
                        successNum++;
                        successMsg.append("<br/>" + successNum + "、楼层 " + airPowerInfo.getFloorName() +"、租户名称"+airPowerInfo.getTenantName()
                                +"、租户面积"+airPowerInfo.getTenantArea()+"、空调公用电"+airPowerInfo.getPublicPower()+"、空调公用电分摊"+airPowerInfo.getAvgPubPower()
                                +"、空调电分摊"+airPowerInfo.getAirPublicPower()+"、分摊金额"+airPowerInfo.getAvgAirPubPow()+ " 导入成功");
                    } else if (isUpdateSupport)
                    {
                        airPowerInfo.setPowerUUID(list.get(0).getPowerUUID());
                        airPowerInfo.setUpdateBy(operName);
                        this.updeteAirPowerInfo(airPowerInfo);
                        successNum++;
                        successMsg.append("<br/>" + successNum + "、楼层 " + airPowerInfo.getFloorName() +"、租户名称"+airPowerInfo.getTenantName()
                                +"、租户面积"+airPowerInfo.getTenantArea()+"、空调公用电"+airPowerInfo.getPublicPower()+"、空调公用电分摊"+airPowerInfo.getAvgPubPower()
                                +"、空调电分摊"+airPowerInfo.getAirPublicPower()+"、分摊金额"+airPowerInfo.getAvgAirPubPow()+ " 导入成功");
                    }
                    else {
                        failureNum++;
                        failureMsg.append("<br/>" + successNum + "、楼层 " + airPowerInfo.getFloorName() +"、租户名称"+airPowerInfo.getTenantName()
                                +"、租户面积"+airPowerInfo.getTenantArea()+"、空调公用电"+airPowerInfo.getPublicPower()+"、空调公用电分摊"+airPowerInfo.getAvgPubPower()
                                +"、空调电分摊"+airPowerInfo.getAirPublicPower()+"、分摊金额"+airPowerInfo.getAvgAirPubPow()+ " 导入成功");
                    }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + successNum + "、楼层 " + airPowerInfo.getFloorName() +"、租户名称"+airPowerInfo.getTenantName()
                        +"、租户面积"+airPowerInfo.getTenantArea()+"、空调公用电"+airPowerInfo.getPublicPower()+"、空调公用电分摊"+airPowerInfo.getAvgPubPower()
                        +"、空调电分摊"+airPowerInfo.getAirPublicPower()+"、分摊金额"+airPowerInfo.getAvgAirPubPow()+ " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        return successMsg.toString();
    }

    @Override
    public AirPowerInfo listAllPowerInfo(AirPowerInfo powerInfo) {
        return airPowerInfoMapper.listAllPowerInfo(powerInfo);
    }
}
